package com.business.system.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import me.chanjar.weixin.mp.bean.result.WxMpUser;
import net.sf.rose.jdbc.DBUtils;
import net.sf.rose.jdbc.service.Service;

import com.business.system.bean.WxUserBean;
import com.business.system.service.WxUserService;

/**
 * @author: xudy
 * @date: 2018/04/07 17:37
 * @description:
 */
@org.springframework.stereotype.Service
public class WxUserServiceImpl implements WxUserService{
    @Override
    public boolean insertWxUserFromWxMpUser(Service service, WxMpUser mpUser) throws Exception {
        WxUserBean user = new WxUserBean();
        user.setCity(mpUser.getCity());
        user.setCountry(mpUser.getCountry());
        user.setOpenId(mpUser.getOpenId());
        user.setGroupId(mpUser.getGroupId());
        user.setHeadimgUrl(mpUser.getHeadImgUrl());
        user.setNickname(mpUser.getNickname());
        if(mpUser.getPrivileges()!=null){
            user.setPrivileges(Arrays.toString(mpUser.getPrivileges()));
        }
        user.setProvince(mpUser.getProvince());
        user.setRemark(mpUser.getRemark());
        user.setSex(mpUser.getSex());
        //2-已关注 1-未关注
        user.setSubscribe(mpUser.getSubscribe()?2:1);
        user.setSubscribeTime(new Date());
        if(mpUser.getTagIds()!=null){
            user.setTagIds(Arrays.toString(mpUser.getTagIds()));
        }
        //推荐人二维码code(采用时间戳的16进制)
        user.setQrcode(Long.toHexString(System.currentTimeMillis()));
        user.setUnionId(mpUser.getUnionId());
        return DBUtils.update(service,user)>0;
    }

    @Override
    public WxUserBean getUserInfoByOpenId(Service service, String openId) throws Exception {
        Map<String,Object> params = new HashMap<>();
        params.put("openId",openId);
        WxUserBean bran = DBUtils.getBean(service,WxUserBean.class,params);
        return bran;
    }

    @Override
    public WxUserBean getUserById(Service service, String id) throws Exception {
        return DBUtils.getBean(service,WxUserBean.class,id);
    }
}